<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>函数 f(x) = sin(x)/x 图像可视化</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            color: #333;
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        header {
            text-align: center;
            margin-bottom: 40px;
            padding-bottom: 20px;
            border-bottom: 2px solid #eaeaea;
        }
        
        h1 {
            font-size: 2.5rem;
            margin-bottom: 15px;
            color: #2c3e50;
        }
        
        .subtitle {
            font-size: 1.2rem;
            color: #7f8c8d;
            max-width: 800px;
            margin: 0 auto;
            line-height: 1.6;
        }
        
        .graph-section {
            margin-bottom: 50px;
            padding: 25px;
            background: #f8f9fa;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
        }
        
        h2 {
            font-size: 1.8rem;
            margin-bottom: 20px;
            color: #3498db;
            display: flex;
            align-items: center;
        }
        
        h2 i {
            margin-right: 10px;
        }
        
        .graph-container {
            position: relative;
            height: 400px;
            margin-top: 20px;
        }
        
        .description {
            margin-top: 20px;
            padding: 15px;
            background: white;
            border-radius: 8px;
            border-left: 4px solid #3498db;
        }
        
        .math-expression {
            font-family: 'Cambria Math', serif;
            font-size: 1.4rem;
            text-align: center;
            margin: 15px 0;
            color: #2c3e50;
        }
        
        .comparison {
            display: flex;
            flex-wrap: wrap;
            gap: 30px;
            margin-top: 40px;
        }
        
        .comparison-item {
            flex: 1;
            min-width: 300px;
            padding: 20px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
        }
        
        .comparison-title {
            font-size: 1.3rem;
            margin-bottom: 15px;
            color: #e74c3c;
            text-align: center;
        }
        
        .key-point {
            background: #e8f4fc;
            padding: 12px;
            border-radius: 6px;
            margin: 10px 0;
            border-left: 3px solid #3498db;
        }
        
        .point-title {
            font-weight: bold;
            color: #2980b9;
        }
        
        .discontinuity-marker {
            position: absolute;
            width: 20px;
            height: 20px;
            border: 3px solid #e74c3c;
            border-radius: 50%;
            background: white;
            transform: translate(-50%, -50%);
            pointer-events: none;
        }
        
        footer {
            text-align: center;
            margin-top: 50px;
            padding-top: 20px;
            border-top: 1px solid #eaeaea;
            color: #7f8c8d;
            font-size: 0.9rem;
        }
        
        @media (max-width: 768px) {
            .comparison {
                flex-direction: column;
            }
            
            h1 {
                font-size: 2rem;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>函数 f(x) = sin(x)/x 图像可视化</h1>
            <p class="subtitle">探索该函数在 x=0 处的特殊性质，以及补充定义后的连续性变化</p>
        </header>
        
        <section class="graph-section">
            <h2><i>📈</i> 图像一：原始函数 f(x) = sin(x)/x</h2>
            <div class="math-expression">f(x) = 
                <span style="font-size: 1.6rem;">
                    <sup>sin(x)</sup>&frasl;<sub>x</sub>
                </span>
                , x ≠ 0
            </div>
            
            <div class="graph-container">
                <canvas id="originalChart"></canvas>
                <div class="discontinuity-marker" id="discontinuityMarker"></div>
            </div>
            
            <div class="description">
                <p><strong>函数特性分析：</strong></p>
                <div class="key-point">
                    <div class="point-title">定义域：</div>
                    x ∈ (-∞, 0) ∪ (0, +∞)，在 x=0 处无定义
                </div>
                <div class="key-point">
                    <div class="point-title">极限性质：</div>
                    lim<sub>x→0</sub> f(x) = 1，但 f(0) 不存在
                </div>
                <div class="key-point">
                    <div class="point-title">零点：</div>
                    x = kπ (k ∈ Z, k ≠ 0)，即 ±π, ±2π, ±3π, ...
                </div>
                <div class="key-point">
                    <div class="point-title">间断点类型：</div>
                    x=0 是可去间断点，因为极限存在但函数值未定义
                </div>
                <div class="key-point">
                    <div class="point-title">图像特征：</div>
                    在 x=0 处有一个"洞"（空心圈表示），函数曲线在此处断开
                </div>
            </div>
        </section>
        
        <section class="graph-section">
            <h2><i>📊</i> 图像二：补充定义 f(0) = 1</h2>
            <div class="math-expression">f(x) = 
                <span style="font-size: 1.6rem;">
                    <sup>sin(x)</sup>&frasl;<sub>x</sub>
                </span>
                , x ≠ 0 &nbsp;&nbsp;&nbsp; f(0) = 1
            </div>
            
            <div class="graph-container">
                <canvas id="definedChart"></canvas>
            </div>
            
            <div class="description">
                <p><strong>函数特性分析：</strong></p>
                <div class="key-point">
                    <div class="point-title">定义域：</div>
                    x ∈ (-∞, +∞)，在 x=0 处有定义 f(0)=1
                </div>
                <div class="key-point">
                    <div class="point-title">连续性：</div>
                    函数在 x=0 处连续，因为 lim<sub>x→0</sub> f(x) = f(0) = 1
                </div>
                <div class="key-point">
                    <div class="point-title">零点：</div>
                    x = kπ (k ∈ Z, k ≠ 0)，即 ±π, ±2π, ±3π, ...
                </div>
                <div class="key-point">
                    <div class="point-title">函数性质：</div>
                    在整个实数域上连续，是可去间断点被"修复"的典型例子
                </div>
            </div>
        </section>
        
        <div class="comparison">
            <div class="comparison-item">
                <div class="comparison-title">原始函数特点</div>
                <ul style="padding-left: 20px;">
                    <li>在 x=0 处有"洞"（空心圈）</li>
                    <li>可去间断点</li>
                    <li>定义域不包含 x=0</li>
                    <li>极限存在但函数值未定义</li>
                    <li>函数曲线在 x=0 处断开</li>
                </ul>
            </div>
            
            <div class="comparison-item">
                <div class="comparison-title">补充定义后特点</div>
                <ul style="padding-left: 20px;">
                    <li>在 x=0 处连续</li>
                    <li>间断点被"修复"</li>
                    <li>定义域为整个实数集</li>
                    <li>极限值等于函数值</li>
                    <li>函数曲线在 x=0 处连接</li>
                </ul>
            </div>
        </div>
        
        <footer>
            <p>函数图像可视化 | 数学分析教学工具 | 展示可去间断点的概念</p>
        </footer>
    </div>

    <script>
        // 准备数据点
        function generateDataPoints() {
            const dataLeft = [];  // x < 0 的部分
            const dataRight = []; // x > 0 的部分
            const dataDefined = [];
            
            // 生成 x 从 -4π 到 -0.1 的数据点（左半边）
            for (let x = -4 * Math.PI; x <= -0.1; x += 0.1) {
                const y = Math.sin(x) / x;
                dataLeft.push({x: x, y: y});
                dataDefined.push({x: x, y: y});
            }
            
            // 生成 x 从 0.1 到 4π 的数据点（右半边）
            for (let x = 0.1; x <= 4 * Math.PI; x += 0.1) {
                const y = Math.sin(x) / x;
                dataRight.push({x: x, y: y});
                dataDefined.push({x: x, y: y});
            }
            
            // 为补充定义的图表添加 x=0 的点
            dataDefined.push({x: 0, y: 1});
            
            // 找出零点 (sin(x)/x = 0 当 sin(x)=0 且 x≠0)
            const zeros = [];
            for (let k = -4; k <= 4; k++) {
                if (k !== 0) {
                    zeros.push(k * Math.PI);
                }
            }
            
            return { dataLeft, dataRight, dataDefined, zeros };
        }
        
        // 创建原始函数图表
        function createOriginalChart() {
            const { dataLeft, dataRight, zeros } = generateDataPoints();
            const ctx = document.getElementById('originalChart').getContext('2d');
            
            // 准备零点数据集
            const zeroPoints = zeros.map(x => ({x: x, y: 0}));
            
            const chart = new Chart(ctx, {
                type: 'scatter',
                data: {
                    datasets: [
                        {
                            label: 'f(x) = sin(x)/x (x < 0)',
                            data: dataLeft,
                            borderColor: '#3498db',
                            backgroundColor: 'rgba(52, 152, 219, 0.1)',
                            borderWidth: 2,
                            showLine: true,
                            pointRadius: 0,
                            tension: 0.4,
                            segment: {
                                borderDash: ctx => undefined
                            }
                        },
                        {
                            label: 'f(x) = sin(x)/x (x > 0)',
                            data: dataRight,
                            borderColor: '#3498db',
                            backgroundColor: 'rgba(52, 152, 219, 0.1)',
                            borderWidth: 2,
                            showLine: true,
                            pointRadius: 0,
                            tension: 0.4
                        },
                        {
                            label: '零点',
                            data: zeroPoints,
                            backgroundColor: '#e74c3c',
                            pointRadius: 5,
                            pointStyle: 'circle'
                        },
                        {
                            label: '可去间断点 (x=0)',
                            data: [{x: 0, y: 1}],
                            backgroundColor: 'rgba(231, 76, 60, 0)',
                            borderColor: '#e74c3c',
                            borderWidth: 3,
                            pointRadius: 6,
                            pointStyle: 'circle',
                            pointHoverRadius: 8
                        }
                    ]
                },
                options: {
                    responsive: true,
                    maintainAspectRatio: false,
                    scales: {
                        x: {
                            type: 'linear',
                            position: 'bottom',
                            title: {
                                display: true,
                                text: 'x',
                                font: {
                                    size: 14,
                                    weight: 'bold'
                                }
                            },
                            ticks: {
                                callback: function(value) {
                                    if (value === 0) return '0';
                                    if (value === Math.PI) return 'π';
                                    if (value === -Math.PI) return '-π';
                                    if (value === 2 * Math.PI) return '2π';
                                    if (value === -2 * Math.PI) return '-2π';
                                    if (value === 3 * Math.PI) return '3π';
                                    if (value === -3 * Math.PI) return '-3π';
                                    if (value === 4 * Math.PI) return '4π';
                                    if (value === -4 * Math.PI) return '-4π';
                                    return value.toFixed(1);
                                }
                            },
                            grid: {
                                color: 'rgba(0, 0, 0, 0.1)'
                            }
                        },
                        y: {
                            title: {
                                display: true,
                                text: 'f(x)',
                                font: {
                                    size: 14,
                                    weight: 'bold'
                                }
                            },
                            min: -0.5,
                            max: 1.2,
                            grid: {
                                color: 'rgba(0, 0, 0, 0.1)'
                            }
                        }
                    },
                    plugins: {
                        legend: {
                            position: 'top',
                        },
                        tooltip: {
                            mode: 'index',
                            intersect: false,
                            callbacks: {
                                label: function(context) {
                                    let label = context.dataset.label || '';
                                    if (label) {
                                        label += ': ';
                                    }
                                    if (context.parsed.y !== null) {
                                        if (context.datasetIndex === 3) {
                                            return '可去间断点: x=0 处无定义，但极限为1';
                                        }
                                        label += `(${context.parsed.x.toFixed(2)}, ${context.parsed.y.toFixed(3)})`;
                                    }
                                    return label;
                                }
                            }
                        }
                    }
                }
            });
            
            return chart;
        }
        
        // 创建补充定义后的图表
        function createDefinedChart() {
            const { dataDefined, zeros } = generateDataPoints();
            const ctx = document.getElementById('definedChart').getContext('2d');
            
            // 准备零点数据集
            const zeroPoints = zeros.map(x => ({x: x, y: 0}));
            
            // 特别标记 x=0 点
            const zeroPoint = [{x: 0, y: 1}];
            
            return new Chart(ctx, {
                type: 'scatter',
                data: {
                    datasets: [
                        {
                            label: 'f(x) = sin(x)/x (x≠0), f(0)=1',
                            data: dataDefined,
                            borderColor: '#2ecc71',
                            backgroundColor: 'rgba(46, 204, 113, 0.1)',
                            borderWidth: 2,
                            showLine: true,
                            pointRadius: 0,
                            tension: 0.4
                        },
                        {
                            label: '零点',
                            data: zeroPoints,
                            backgroundColor: '#e74c3c',
                            pointRadius: 5,
                            pointStyle: 'circle'
                        },
                        {
                            label: 'f(0)=1',
                            data: zeroPoint,
                            backgroundColor: '#9b59b6',
                            pointRadius: 6,
                            pointStyle: 'rectRot'
                        }
                    ]
                },
                options: {
                    responsive: true,
                    maintainAspectRatio: false,
                    scales: {
                        x: {
                            type: 'linear',
                            position: 'bottom',
                            title: {
                                display: true,
                                text: 'x',
                                font: {
                                    size: 14,
                                    weight: 'bold'
                                }
                            },
                            ticks: {
                                callback: function(value) {
                                    if (value === 0) return '0';
                                    if (value === Math.PI) return 'π';
                                    if (value === -Math.PI) return '-π';
                                    if (value === 2 * Math.PI) return '2π';
                                    if (value === -2 * Math.PI) return '-2π';
                                    if (value === 3 * Math.PI) return '3π';
                                    if (value === -3 * Math.PI) return '-3π';
                                    if (value === 4 * Math.PI) return '4π';
                                    if (value === -4 * Math.PI) return '-4π';
                                    return value.toFixed(1);
                                }
                            },
                            grid: {
                                color: 'rgba(0, 0, 0, 0.1)'
                            }
                        },
                        y: {
                            title: {
                                display: true,
                                text: 'f(x)',
                                font: {
                                    size: 14,
                                    weight: 'bold'
                                }
                            },
                            min: -0.5,
                            max: 1.2,
                            grid: {
                                color: 'rgba(0, 0, 0, 0.1)'
                            }
                        }
                    },
                    plugins: {
                        legend: {
                            position: 'top',
                        },
                        tooltip: {
                            mode: 'index',
                            intersect: false,
                            callbacks: {
                                label: function(context) {
                                    let label = context.dataset.label || '';
                                    if (label) {
                                        label += ': ';
                                    }
                                    if (context.parsed.y !== null) {
                                        label += `(${context.parsed.x.toFixed(2)}, ${context.parsed.y.toFixed(3)})`;
                                    }
                                    return label;
                                }
                            }
                        }
                    }
                }
            });
        }
        
        // 初始化图表
        document.addEventListener('DOMContentLoaded', function() {
            const originalChart = createOriginalChart();
            createDefinedChart();
            
            // 更新间断点标记的位置
            function updateDiscontinuityMarker() {
                const marker = document.getElementById('discontinuityMarker');
                const canvas = document.getElementById('originalChart');
                const chartArea = originalChart.chartArea;
                
                if (chartArea) {
                    const xPixel = chartArea.left + (0 - originalChart.scales.x.min) / 
                        (originalChart.scales.x.max - originalChart.scales.x.min) * 
                        (chartArea.right - chartArea.left);
                    
                    const yPixel = chartArea.bottom - (1 - originalChart.scales.y.min) / 
                        (originalChart.scales.y.max - originalChart.scales.y.min) * 
                        (chartArea.bottom - chartArea.top);
                    
                    marker.style.left = xPixel + 'px';
                    marker.style.top = yPixel + 'px';
                    marker.style.display = 'block';
                }
            }
            
            // 监听图表渲染完成
            originalChart.canvas.addEventListener('chartjs_render', updateDiscontinuityMarker);
            setTimeout(updateDiscontinuityMarker, 100);
        });
    </script>
</body>
</html>